package com.bristua.user.biz;
import com.bristua.common.enums.CommonCode;
import com.bristua.common.exception.BizException;
import com.bristua.common.utils.CacheUtil;
import com.bristua.user.dao.TAdminUserMapper;
import com.bristua.user.model.TAdminUser;
import com.bristua.user.model.condition.TAdminUserExample;
import com.bristua.user.model.condition.UserLoginParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2019/3/30.
 */
@Service
public class SystemUserLoginBiz {

    @Resource
    private TAdminUserMapper tAdminUserMapper;

    @Autowired
    private CacheUtil cacheUtil;

    public String userLogin(UserLoginParams userLoginParams){
        TAdminUserExample tAdminUserExample = new TAdminUserExample();
        tAdminUserExample.createCriteria().andUserNameEqualTo(userLoginParams.getUserName()).andPasswordEqualTo(userLoginParams.getPassword());
        List<TAdminUser> tAdminUsers = tAdminUserMapper.selectByExample(tAdminUserExample);
        if(tAdminUsers!=null && tAdminUsers.size()==1){
            TAdminUser tAdminUser = tAdminUsers.get(0);
             return cacheUtil.put(tAdminUser.getUserName(), tAdminUser);
        }
        throw new BizException(CommonCode.LOGIN_FAIL.getCode(),CommonCode.LOGIN_FAIL.getMsg());
    }
}
